<?php
class Admin_CruiseController extends Apps_Controller_Action_Admin {
	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbCruise = new M_DbTable_Cruise();
		$grid 	= new Apps_Plugin_Grid('cruiseGrid');

		$grid->setData($tbCruise->getList())
			 ->setTableCaption('List Cruise')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Name (En)',
			 			  'dataField'	=> 'en_name'),
			 		array('title'		=> 'Name (Vi)',
			 			  'dataField'	=> 'name'),
			 		array('title'		=> 'Category',
			 			  'dataField'	=> 'cruise_category_title',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Type',
			 			  'dataField'	=> 'cruise_type_title',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Description',
			 			  'dataField'	=> 'description'),
                    array('title'		=> 'Public',
			 			  'html'	    => function ($rs) {
			 			  						return ($rs['status'] == M_DbTable_Cruise::STATUS_ACTIVE)?
			 			  						'<a href="##" class="ajax-link-must-confirm popup hint  label label-success" data-ajax-to="/admin/cruise/change-value?p=public&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="No">Yes</a>' :
			 			  						'<a href="##" class="ajax-link-must-confirm popup hint  label label-warning" data-ajax-to="/admin/cruise/change-value?p=public&id=' . $rs['id'] . '" data-must-confirm-msg="Do you want to change this ?" title="Thay đổi" data-after-ajax="Yes">No</a>';
											},
			 			  'class'		=> 'center'),
			 		array('title'       => 'Edit',
			 			  'html'        => array('<a href="/admin/cruise/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%')
			 		));
		$grid->applySearch(new Admin_Form_Search_Cruise());

		$this->view->grid = $grid;
	}

	//ADD
	public function addAction() {
		$rq 		= $this->_request;
		$f 			= new Admin_Form_AddCruise();
		$tbCruise	= new M_DbTable_Cruise();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbCruise->safelyInsert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed
				);
				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT Cruise
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbCruise 	= new M_DbTable_Cruise();
		$tbArticle 	= new M_DbTable_Article();

		$cruise 	= $tbCruise->getCruise($id);

		if (!$id || !$cruise) {
			$this->_helper->redirector('index');
		}

		$f = new Admin_Form_AddCruise();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbCruise->safelyUpdate($f->getValues(), array('id =?' => $id))
					? self::$msgUpdateSuccess
					: self::$msgUpdateFailed
				);
				$this->_helper->redirector('index');
			}
		}

		$f->setLegend('Edit Cruise');
		$f->populate($cruise->query()->fetch());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		if (!$id) {
			$this->_helper->redirector('index');
		}

		$tbCruise = new M_DbTable_Cruise();
		$tbArticle = new M_DbTable_Article();

		if (is_numeric($id)) {
			if ($tbCruise->safelyDelete('id ='. $id)) {
				$this->_flashMessenger->addMessage(
					$tbArticle->deleteArticle($id, $tbArticle::REFERENCE_TYPE_CRUISE)
					? self::$msgDeleteSuccess
					: self::$msgDeleteError
				);
			}
		} elseif (is_array($id)) {
			foreach ($id as $v) {
				if ($tbCruise->safelyDelete('id ='. $v)) {
					$tbArticle->deleteArticle($v, $tbArticle::REFERENCE_TYPE_CRUISE);
				}
			}
		}

		$this->_helper->redirector('index');
	}

	public function changeValueAction() {
		$this->disableLayout(true, true);
		$rq = $this->_request;
		$param  = $rq->getParam('p');
		$id 	= intval($rq->getParam('id'));
		$tbCruise = new M_DbTable_Cruise();
		if(!($r = $tbCruise->fetchRow(array('id = ?' => $id))->toArray())) {
			echo '<script>window.location=window.location</script>
				<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
				. $this->getBacktrackUrl()
				. '">Update Error (Record not found), please wait ...</a>';
		}

		switch ($param) {
			case 'offer':
				($r['special_offer'] == M_DbTable_Cruise::SPECIAL_OFFER_TRUE) ?
					$tbCruise->safelyUpdate(array('special_offer' 		=> M_DbTable_Cruise::SPECIAL_OFFER_FALSE),
										   array('special_offer = ?' 	=> M_DbTable_Cruise::SPECIAL_OFFER_TRUE,
										   		 'id =?'				=> $id)) :
					$tbCruise->safelyUpdate(array('special_offer' 		=> M_DbTable_Cruise::SPECIAL_OFFER_TRUE),
										   array('special_offer = ?' 	=> M_DbTable_Cruise::SPECIAL_OFFER_FALSE,
										   		 'id =?'				=> $id));
				break;

			case 'public':
			    ($r['status'] == M_DbTable_Cruise::STATUS_ACTIVE) ?
    			    $tbCruise->safelyUpdate(array('status'  => M_DbTable_Cruise::STATUS_INACTIVE),
    			                         array('id =?'	  => $id)) :
    			    $tbCruise->safelyUpdate(array('status'  => M_DbTable_Cruise::STATUS_ACTIVE),
    			                        array('id =?'  => $id));
			    break;


			default: break;
		}

		echo '<script>location.href = location.href</script>
			<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
			. $this->getBacktrackUrl()
			. '">Update successfully, please wait ...</a>';
	}
}